package cameraUI;

import static com.googlecode.javacv.cpp.opencv_core.cvFlip;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_core.*;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import projectUtils.ColorUtil;

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.FrameGrabber;
import com.googlecode.javacv.VideoInputFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class CameraCapture implements Runnable {
	// final int INTERVAL=1000;///you may use interval
	IplImage image;
	CanvasFrame imageCanvas = new CanvasFrame("Web Cam");
	CanvasFrame imageCanvas2 = new CanvasFrame("Web Cam 2");
	CanvasFrame tokenCanvas = new CanvasFrame("Token");
	CanvasFrame resultCanvas = new CanvasFrame("Result");

	public CameraCapture() {
		imageCanvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
		tokenCanvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
		resultCanvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
	}

	@Override
	public void run() {
		FrameGrabber grabber = new VideoInputFrameGrabber(0);
		int i = 0;

		try {
			grabber.start();
		} catch (com.googlecode.javacv.FrameGrabber.Exception e1) {
			e1.printStackTrace();
		}
		IplImage img;
		IplImage resizedImage;
		IplImage blurredImage;
		IplImage backgroundRemovedImage;
		DigitRecognition digitRecognition = new DigitRecognition();
		while (true) {
			try {
				img = grabber.grab();
				resizedImage = cvCreateImage(cvSize(300, 300), img.depth(),
						img.nChannels());
				backgroundRemovedImage = cvCreateImage(cvSize(300, 300),
						img.depth(), img.nChannels());

				BufferedImage resultDigitImage = new BufferedImage(29, 29,
						BufferedImage.TYPE_INT_RGB);
				for (int x = 0; x < 29; x++) {
					for (int y = 0; y < 29; y++) {
						resultDigitImage.setRGB(x, y, 0xFFFFFFFF);
					}
				}

				if (img != null) {
					cvFlip(img, img, 1);// l-r = 90_degrees_steps_anti_clockwise
					// cvSaveImage((i++)+"-capture.jpg", img);
					cvResize(img, resizedImage);
					blurredImage = resizedImage.clone();
					cvSmooth(blurredImage, blurredImage, CV_BLUR, 100);
					cvAbsDiff(resizedImage, blurredImage,
							backgroundRemovedImage);

					BufferedImage bImage = backgroundRemovedImage
							.getBufferedImage();
					BufferedImage resultImage = new BufferedImage(
							bImage.getWidth(), bImage.getHeight(),
							BufferedImage.TYPE_INT_BGR);
					for (int x = 0; x < bImage.getWidth(); x++) {
						for (int y = 0; y < bImage.getHeight(); y++) {
							int intensity = 255 - ColorUtil
									.RGBToGreyScale(bImage.getRGB(x, y));
							if (intensity < 220) {
								resultImage.setRGB(bImage.getWidth() - x - 1,
										y, new Color(intensity, intensity,
												intensity).getRGB());
							} else {
								resultImage.setRGB(bImage.getWidth() - x - 1,
										y, 0xFFFFFFFF);
							}
						}
					}

					cvFlip(img, img, 1);
					imageCanvas.showImage(img);

					int result = digitRecognition.Recognize(resultImage);
					
					imageCanvas2.showImage(resultImage);
					// show image on window
					tokenCanvas.showImage(digitRecognition.resultImage);

					Graphics g = resultDigitImage.getGraphics();
					g.setColor(Color.BLACK);
					g.setFont(new Font("Serif", Font.PLAIN, 18));
					g.drawString(result + "", 10, 20);
					// release resources
					g.dispose();

					resultCanvas.showImage(resultDigitImage);

				}

			} catch (Exception e) {
				e.printStackTrace();
			}
			// Thread.sleep(50);
		}
	}
}
